home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / metamail / tahoe / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-18  |  6.8 KB  |  358 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms are permitted
  6.  * provided that this notice is preserved and that due credit is given
  7.  * to the University of California at Berkeley. The name of the University
  8.  * may not be used to endorse or promote products derived from this
  9.  * software without specific prior written permission. This software
  10.  * is provided ``as is'' without express or implied warranty.
  11.  */
  12.  
  13. #ifdef notdef
  14. char copyright[] =
  15. "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  16.  All rights reserved.\n";
  17. #endif /* notdef */
  18.  
  19. #ifdef notdef
  20. static char sccsid[] = "@(#)main.c    5.5 (Berkeley) 2/18/88";
  21. #endif /* notdef */
  22.  
  23. #include "rcv.h"
  24. #include <sys/stat.h>
  25.  
  26. /*
  27.  * Mail -- a mail program
  28.  *
  29.  * Startup -- interface with user.
  30.  */
  31.  
  32. jmp_buf    hdrjmp;
  33.  
  34. /*
  35.  * Find out who the user is, copy his mail file (if exists) into
  36.  * /tmp/Rxxxxx and set up the message pointers.  Then, print out the
  37.  * message headers and read user commands.
  38.  *
  39.  * Command line syntax:
  40.  *    Mail [ -i ] [ -r address ] [ -h number ] [ -f [ name ] ]
  41.  * or:
  42.  *    Mail [ -i ] [ -r address ] [ -h number ] people ...
  43.  */
  44.  
  45. main(argc, argv)
  46.     char **argv;
  47. {
  48.     register char *ef;
  49.     register int i, argp;
  50.     int mustsend, hdrstop(), (*prevint)(), f;
  51.     struct sgttyb tbuf;
  52.  
  53.     /*
  54.      * Set up a reasonable environment.
  55.      * Figure out whether we are being run interactively, set up
  56.      * all the temporary files, buffer standard output, and so forth.
  57.      */
  58.  
  59. #ifdef    GETHOST
  60.     inithost();
  61. #endif    GETHOST
  62.     mypid = getpid();
  63.     intty = isatty(0);
  64.     outtty = isatty(1);
  65.     if (outtty) {
  66.         gtty(1, &tbuf);
  67.         baud = tbuf.sg_ospeed;
  68.     } else
  69.         baud = B9600;
  70.     image = -1;
  71.  
  72.     /*
  73.      * Now, determine how we are being used.
  74.      * We successively pick off instances of -r, -h, -f, and -i.
  75.      * If called as "rmail" we note this fact for letter sending.
  76.      * If there is anything left, it is the base of the list
  77.      * of users to mail to.  Argp will be set to point to the
  78.      * first of these users.
  79.      */
  80.  
  81.     ef = NOSTR;
  82.     argp = -1;
  83.     mustsend = 0;
  84.     if (argc > 0 && **argv == 'r')
  85.         rmail++;
  86.     for (i = 1; i < argc; i++) {
  87.  
  88.         /*
  89.          * If current argument is not a flag, then the
  90.          * rest of the arguments must be recipients.
  91.          */
  92.  
  93.         if (*argv[i] != '-') {
  94.             argp = i;
  95.             break;
  96.         }
  97.         switch (argv[i][1]) {
  98.         case 'r':
  99.             /*
  100.              * Next argument is address to be sent along
  101.              * to the mailer.
  102.              */
  103.             if (i >= argc - 1) {
  104.                 fprintf(stderr, "Address required after -r\n");
  105.                 exit(1);
  106.             }
  107.             mustsend++;
  108.             rflag = argv[i+1];
  109.             i++;
  110.             break;
  111.  
  112.         case 'T':
  113.             /*
  114.              * Next argument is temp file to write which
  115.              * articles have been read/deleted for netnews.
  116.              */
  117.             if (i >= argc - 1) {
  118.                 fprintf(stderr, "Name required after -T\n");
  119.                 exit(1);
  120.             }
  121.             Tflag = argv[i+1];
  122.             if ((f = creat(Tflag, 0600)) < 0) {
  123.                 perror(Tflag);
  124.                 exit(1);
  125.             }
  126.             close(f);
  127.             i++;
  128.             break;
  129.  
  130.         case 'u':
  131.             /*
  132.              * Next argument is person to pretend to be.
  133.              */
  134.             if (i >= argc - 1) {
  135.                 fprintf(stderr, "Missing user name for -u\n");
  136.                 exit(1);
  137.             }
  138.             strcpy(myname, argv[i+1]);
  139.             i++;
  140.             break;
  141.  
  142.         case 'i':
  143.             /*
  144.              * User wants to ignore interrupts.
  145.              * Set the variable "ignore"
  146.              */
  147.             assign("ignore", "");
  148.             break;
  149.  
  150.         case 'd':
  151.             debug++;
  152.             break;
  153.  
  154.         case 'h':
  155.             /*
  156.              * Specified sequence number for network.
  157.              * This is the number of "hops" made so
  158.              * far (count of times message has been
  159.              * forwarded) to help avoid infinite mail loops.
  160.              */
  161.             if (i >= argc - 1) {
  162.                 fprintf(stderr, "Number required for -h\n");
  163.                 exit(1);
  164.             }
  165.             mustsend++;
  166.             hflag = atoi(argv[i+1]);
  167.             if (hflag == 0) {
  168.                 fprintf(stderr, "-h needs non-zero number\n");
  169.                 exit(1);
  170.             }
  171.             i++;
  172.             break;
  173.  
  174.         case 's':
  175.             /*
  176.              * Give a subject field for sending from
  177.              * non terminal
  178.              */
  179.             if (i >= argc - 1) {
  180.                 fprintf(stderr, "Subject req'd for -s\n");
  181.                 exit(1);
  182.             }
  183.             mustsend++;
  184.             sflag = argv[i+1];
  185.             i++;
  186.             break;
  187.  
  188.         case 'f':
  189.             /*
  190.              * User is specifying file to "edit" with Mail,
  191.              * as opposed to reading system mailbox.
  192.              * If no argument is given after -f, we read his
  193.              * mbox file in his home directory.
  194.              */
  195.             if (i >= argc - 1)
  196.                 ef = mbox;
  197.             else
  198.                 ef = argv[i + 1];
  199.             i++;
  200.             break;
  201.  
  202.         case 'n':
  203.             /*
  204.              * User doesn't want to source /usr/lib/Mail.rc
  205.              */
  206.             nosrc++;
  207.             break;
  208.  
  209.         case 'N':
  210.             /*
  211.              * Avoid initial header printing.
  212.              */
  213.             noheader++;
  214.             break;
  215.  
  216.         case 'v':
  217.             /*
  218.              * Send mailer verbose flag
  219.              */
  220.             assign("verbose", "");
  221.             break;
  222.  
  223.         case 'I':
  224.             /*
  225.              * We're interactive
  226.              */
  227.             intty = 1;
  228.             break;
  229.  
  230.         default:
  231.             fprintf(stderr, "Unknown flag: %s\n", argv[i]);
  232.             exit(1);
  233.         }
  234.     }
  235.  
  236.     /*
  237.      * Check for inconsistent arguments.
  238.      */
  239.  
  240.     if (ef != NOSTR && argp != -1) {
  241.         fprintf(stderr, "Cannot give -f and people to send to.\n");
  242.         exit(1);
  243.     }
  244.     if (mustsend && argp == -1) {
  245.         fprintf(stderr, "The flags you gave make no sense since you're not sending mail.\n");
  246.         exit(1);
  247.     }
  248.     tinit();
  249.     setscreensize();
  250.     input = stdin;
  251.     rcvmode = argp == -1;
  252.     if (!nosrc)
  253.         load(MASTER);
  254.     load(mailrc);
  255.     if (argp != -1) {
  256.         mail(&argv[argp]);
  257.  
  258.         /*
  259.          * why wait?
  260.          */
  261.  
  262.         exit(senderr);
  263.     }
  264.  
  265.     /*
  266.      * Ok, we are reading mail.
  267.      * Decide whether we are editing a mailbox or reading
  268.      * the system mailbox, and open up the right stuff.
  269.      */
  270.  
  271.     if (ef != NOSTR) {
  272.         char *ename;
  273.  
  274.         edit++;
  275.         ename = expand(ef);
  276.         if (ename != ef) {
  277.             ef = malloc((unsigned) strlen(ename) + 1);
  278.             strcpy(ef, ename);
  279.         }
  280.         editfile = ef;
  281.         strcpy(mailname, ef);
  282.     }
  283.     if (setfile(mailname, edit) < 0) {
  284.         if (edit)
  285.             perror(mailname);
  286.         else
  287.             fprintf(stderr, "No mail for %s\n", myname);
  288.         exit(1);
  289.     }
  290.     if (!noheader && value("noheader") == NOSTR) {
  291.         if (setjmp(hdrjmp) == 0) {
  292.             if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
  293.                 signal(SIGINT, hdrstop);
  294.             announce(!0);
  295.             fflush(stdout);
  296.             signal(SIGINT, prevint);
  297.         }
  298.     }
  299.     if (!edit && msgCount == 0) {
  300.         printf("No mail\n");
  301.         fflush(stdout);
  302.         exit(0);
  303.     }
  304.     commands();
  305.     if (!edit) {
  306.         signal(SIGHUP, SIG_IGN);
  307.         signal(SIGINT, SIG_IGN);
  308.         signal(SIGQUIT, SIG_IGN);
  309.         quit();
  310.     }
  311.     exit(0);
  312. }
  313.  
  314. /*
  315.  * Interrupt printing of the headers.
  316.  */
  317. hdrstop()
  318. {
  319.  
  320.     fflush(stdout);
  321.     fprintf(stderr, "\nInterrupt\n");
  322.     longjmp(hdrjmp, 1);
  323. }
  324.  
  325. /*
  326.  * Compute what the screen size should be.
  327.  * We use the following algorithm for the height:
  328.  *    If baud rate < 1200, use  9
  329.  *    If baud rate = 1200, use 14
  330.  *    If baud rate > 1200, use 24 or ws_row
  331.  * Width is either 80 or ws_col;
  332.  */
  333. setscreensize()
  334. {
  335. #ifdef    TIOCGWINSZ
  336.     struct winsize ws;
  337.  
  338.     if (ioctl(fileno(stdout), TIOCGWINSZ, (char *) &ws) < 0)
  339.         ws.ws_col = ws.ws_row = 0;
  340. #endif
  341.     if (baud < B1200)
  342.         screenheight = 9;
  343.     else if (baud == B1200)
  344.         screenheight = 14;
  345. #ifdef    TIOCGWINSZ
  346.     else if (ws.ws_row != 0)
  347.         screenheight = ws.ws_row;
  348. #endif
  349.     else
  350.         screenheight = 24;
  351. #ifdef TIOCGWINSZ
  352.     if (ws.ws_col != 0)
  353.         screenwidth = ws.ws_col;
  354.     else
  355. #endif
  356.         screenwidth = 80;
  357. }
  358.